﻿using D.B.Data.API.Applications.Command.LeaveCommand;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler.LeaveCommandHandler
{
    public class DelLeaveCommandHandler : IRequestHandler<DelLeaveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<WorkflowModel> baseRepository;

        public DelLeaveCommandHandler(IBaseRepository<WorkflowModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public Task<APIResult<int>> Handle(DelLeaveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> aPIResult = new APIResult<int>();
            var res= baseRepository.GetAll().FirstOrDefault(s=>s.Id==request.Id);
            if(res.State==true)
            {
                aPIResult.Code=EnumCode.File;
                aPIResult.Msg = "已审批不能删除";
            }
            else
            {
                res.IsDel = true;
                baseRepository.Update(res);
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "删除成功";
            }
            
            return Task.FromResult(aPIResult);
        }
    }
}
